package org.mparts.server;

import com.gargoylesoftware.htmlunit.AjaxController;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet that makes this application crawlable.
 */
@Singleton
public final class CrawlFilter implements Filter {

  /**
   * Special URL token that gets passed from the crawler to the servlet filter.
   * This token is used in case there are already existing query parameters.
   */
  private static final String ESCAPED_FRAGMENT_FORMAT1 = "_escaped_fragment_=";
  private static final int ESCAPED_FRAGMENT_LENGTH1 = ESCAPED_FRAGMENT_FORMAT1.length();
  /**
   * Special URL token that gets passed from the crawler to the servlet filter.
   * This token is used in case there are not already existing query parameters.
   */
  private static final String ESCAPED_FRAGMENT_FORMAT2 = "&"+ESCAPED_FRAGMENT_FORMAT1;
  private static final int ESCAPED_FRAGMENT_LENGTH2 = ESCAPED_FRAGMENT_FORMAT2.length();

//  @Inject(optional = true)
  private final Provider<WebClient> webClientProvider = null;

//  @Inject(optional = true)
  private long timeoutMillis = 10000;

  private Logger log = Logger.getLogger(this.getClass().getName());;
  
//  @Inject
//  public CrawlFilter( Logger log ) {
//    this.log = log;
//  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  }

  /**
   * Destroys the filter configuration.
   */
  @Override
  public void destroy() {
  }

  /**
   * Filters all requests and invokes headless browser if necessary.
   */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    String queryString = req.getQueryString();

    // Only process calls to the main HTML page, and the empty one if desired
    final String requestURI = req.getRequestURI();
    log.warning("process request: " + queryString);
    log.warning("process request URI: " + requestURI);

    // Does this request contain an _escaped_fragment_?
    if ((queryString != null) && (queryString.contains(ESCAPED_FRAGMENT_FORMAT1))) {
      try {
        StringBuilder pageNameSb = new StringBuilder("http://");
        pageNameSb.append(req.getServerName());
        if (req.getServerPort() != 0) {
          pageNameSb.append(":");
          pageNameSb.append(req.getServerPort());
        }
        pageNameSb.append(requestURI);
        queryString = rewriteQueryString(queryString);
        pageNameSb.append(queryString);        
        String pageName = pageNameSb.toString();
        //redirect to crawler service
//        pageName = "http://crawlservice.appspot.com/?key=123456&url=http://avtoimpuls.com#!catalog";// + pageName;
  
        log.warning( "Crawl filter encountered escaped fragment, will open: " + pageName );
  
        WebClient webClient;
        if( webClientProvider == null )
          webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        else
          webClient = webClientProvider.get();
  

    webClient.setCssEnabled(false);
    webClient.setJavaScriptTimeout(0);
    webClient.setJavaScriptTimeout(0);
    webClient.setThrowExceptionOnScriptError(false);
    webClient.setThrowExceptionOnFailingStatusCode(false);
    webClient.setJavaScriptEnabled(true);
    HtmlPage page = webClient.getPage(pageName);
    webClient.getJavaScriptEngine().pumpEventLoop(timeoutMillis);

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("<hr />\n");
    stringBuilder.append("<center><h3>You are viewing a non-interactive page that is intended for the crawler.  ");
    stringBuilder.append("You probably want to see this page: <a href=\"" + pageName + "\">" + pageName + "</a></h3></center>\n");
    stringBuilder.append("<hr />\n");

    stringBuilder.append(page.asXml());
    webClient.closeAllWindows();
//    return stringBuilder;
    
//        webClient.setThrowExceptionOnScriptError(false);
//        webClient.setJavaScriptEnabled(true);
//        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
////        webClient.setAjaxController(new AjaxController() {
////
////			@Override
////			public boolean processSynchron(HtmlPage page, WebRequest request,
////					boolean async) {
////				return true;
////			}
////        	
////        });
//        HtmlPage page = webClient.getPage( pageName );
//        int i = webClient.waitForBackgroundJavaScript( timeoutMillis );
////        while (i > 0) {
////            i = webClient.waitForBackgroundJavaScript(1000);
////
////            if (i == 0)
////            {
////                break;
////            }
////            synchronized (page) 
////            {
////                System.out.println("wait");
////                page.wait(500);
////            }
////        }  
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        out.println(stringBuilder.toString());
//        out.println("<hr />");
//        out.println("<center><h3>You are viewing a non-interactive page that is intended for the crawler.  "
//            + "You probably want to see this page: <a href=\""
//            + pageName
//            + "\">"
//            + pageName + "</a></h3></center>");
//        out.println("<hr />");
//        
//        out.println(page.asXml());
//        webClient.closeAllWindows();
//  
        out.println("");
        out.close();
      }
      catch( Exception e ) {
        log.log(Level.SEVERE, "Crawl filter encountered an exception.", e);
//        Utils.logStackTrace(log, e);
      }
      
      
      log.info( "Crawl filter exiting, no chaining." );
    } else {
      chain.doFilter(request, response);
    }
  }


  /**
   * Maps from the query string that contains _escaped_fragment_ to one that
   * doesn't, but is instead followed by a hash fragment. It also unescapes any
   * characters that were escaped by the crawler. If the query string does not
   * contain _escaped_fragment_, it is not modified.
   * 
   * @param queryString
   * @return A modified query string followed by a hash fragment if applicable.
   *         The non-modified query string otherwise.
   * @throws UnsupportedEncodingException
   */
  private static String rewriteQueryString(String queryString)
  throws UnsupportedEncodingException {
    int index = queryString.indexOf(ESCAPED_FRAGMENT_FORMAT2);
    int length = ESCAPED_FRAGMENT_LENGTH2;
    if (index == -1) {
      index = queryString.indexOf(ESCAPED_FRAGMENT_FORMAT1);
      length = ESCAPED_FRAGMENT_LENGTH1;
    }
    if (index != -1) {
      StringBuilder queryStringSb = new StringBuilder();
      if (index > 0) {
        queryStringSb.append("?");
        queryStringSb.append(queryString.substring(0, index));
      }
      queryStringSb.append("#!");
      queryStringSb.append(URLDecoder.decode(queryString.substring(index
          + length, queryString.length()), "UTF-8"));
      return queryStringSb.toString();
    }
    return queryString;
  }

	
	
	
	
	
	
}
